package com.example.administrator.dialogalert;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Builder dialog = new AlertDialog.Builder(MainActivity.this);//设置一个Builder对象
        dialog.setIcon(android.R.drawable.ic_dialog_info);
        dialog.setTitle("欢迎"); //设置标题
        dialog.setMessage("欢迎使用本程序");//设置显示文本
        dialog.setPositiveButton("YES", new OnClickListener() {
            public void onClick(DialogInterface a0, int a1) {
                // 确定完成的事情
                Toast toast = Toast.makeText(MainActivity.this, "你点击了确定按钮", Toast.LENGTH_SHORT);
                toast.show();
            }
        });

        dialog.setNegativeButton("NO", new OnClickListener() {
            public void onClick(DialogInterface a0, int a1) {
                // 确定否定的事情
                Toast toast = Toast.makeText(MainActivity.this, "你点击了否定按钮", Toast.LENGTH_SHORT);
                toast.show();
            }

        });
        dialog.create();
        dialog.show();
    }
}
